package com.ssyt.ec.exam.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.ssyt.ec.common.AppServlet;
import com.ssyt.ec.common.PageModel;
import com.ssyt.ec.common.util.FastjsonUtil;
import com.ssyt.ec.common.util.SysuserUtil;
import com.ssyt.ec.exam.entity.ExamPaperEntity;
import com.ssyt.ec.exam.entity.ExamPlanEntity;
import com.ssyt.ec.exam.service.ExamPlanService;
import com.ssyt.ec.stu.entity.ClassEntity;
import com.ssyt.ec.stu.entity.UserEntity;

@WebServlet("/exam/ExamPlanServlet")
public class ExamPlanServlet extends AppServlet {
	private static final long serialVersionUID = 1L;
	private ExamPlanService examPlanService = new ExamPlanService();

	public ExamPlanServlet() {
		super();
	}

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String command = request.getParameter("command");
		if ("listExamPlan".equals(command)) {
			listExamPlan(request, response);
		} else if ("listUser".equals(command)) {
			listUser(request, response);
		} else if ("listPaper".equals(command)) {
			listPaper(request, response);
		} else if ("listClass".equals(command)) {
			listClass(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else if ("getExamPlan".equals(command)) {
			getExamPlan(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("open".equals(command)) {
			open(request, response);
		}
	}

	/**
	 * 启用
	 * 
	 * @param request
	 * @param response
	 */
	private void open(HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("ids");
		int userId = SysuserUtil.getUserId(request);
		int row = examPlanService.open(ids, userId);
		if (row != 0) {
			print(response, row);
		}
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("ids");
		int userId = SysuserUtil.getUserId(request);
		int row = examPlanService.delete(ids, userId);
		if (row != 0) {
			print(response, row);
		}
	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param response
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String examTimeStart = request.getParameter("examTimeStart");
		String examTimeStop = request.getParameter("examTimeStop");
		String examClassroom = request.getParameter("examClassroom");
		String examPaperName = request.getParameter("examPaperName");
		String[] toUserIds = request.getParameterValues("examPlanToUser");
		String toClassIds = request.getParameter("examPlanToClass");
		String examPlanState = request.getParameter("examPlanState");
		String memo = request.getParameter("memo");
		int userId = SysuserUtil.getUserId(request);
		String toUserIdsStr = "";
		if (StringUtils.isNoneEmpty(toUserIds)) {
			toUserIdsStr = "," + StringUtils.join(toUserIds, ",") + ",";
		}

		List<Object> paramList = new ArrayList<>();
		paramList.add(examTimeStart);
		paramList.add(examTimeStop);
		paramList.add(examClassroom);
		paramList.add(examPaperName);
		paramList.add(toUserIdsStr);
		paramList.add(toClassIds);
		paramList.add(userId);
		paramList.add(examPlanState);
		paramList.add(memo);
		paramList.add(id);

		int row = examPlanService.update(paramList);
		print(response, row);
	}

	/**
	 * 按id查询考试安排
	 * 
	 * @param request
	 * @param response
	 */
	private void getExamPlan(HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		int userId = SysuserUtil.getUserId(request);
		ExamPlanEntity examPlanEntity = examPlanService.getExamPlan(id);
		examPlanEntity.setUserId(userId);
		print(response, FastjsonUtil.toJsonWithDateFormat(examPlanEntity,
				"yyyy-MM-dd HH:mm"));
	}

	/**
	 * 考试安排新增
	 * 
	 * @param request
	 * @param response
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) {
		String examTimeStart = request.getParameter("examTimeStart");
		String examTimeStop = request.getParameter("examTimeStop");
		String examClassroom = request.getParameter("examClassroom");
		String examPaperName = request.getParameter("examPaperName");
		String[] toUserIds = request.getParameterValues("examPlanToUser");
		int toClassId = 0;
		String toClassIds = request.getParameter("examPlanToClass");
		if (StringUtils.isNotBlank(toClassIds)) {
			toClassId = Integer.parseInt(toClassIds);
		}
		String examPlanState = request.getParameter("examPlanState");
		String memo = request.getParameter("memo");
		int userId = SysuserUtil.getUserId(request);
		String toUserIdsStr = "";
		if (StringUtils.isNoneEmpty(toUserIds)) {
			toUserIdsStr = "," + StringUtils.join(toUserIds, ",") + ",";
		}

		List<Object> paramList = new ArrayList<>();
		paramList.add(examTimeStart);
		paramList.add(examTimeStop);
		paramList.add(examClassroom);
		paramList.add(examPaperName);
		paramList.add(toUserIdsStr);
		paramList.add(toClassId);
		paramList.add(userId);
		paramList.add(examPlanState);
		paramList.add(memo);

		long examPlanId = examPlanService.add(paramList);
		if (examPlanId != 0) {
			// 得到本次新增考试安排的Id
			print(response, examPlanId);
		}
	}

	/**
	 * 班级查询
	 * 
	 * @param request
	 * @param response
	 */
	private void listClass(HttpServletRequest request,
			HttpServletResponse response) {
		List<ClassEntity> classList = examPlanService.listClass();
		print(response, FastjsonUtil.toJson(classList));
	}

	/**
	 * 试卷查询
	 * 
	 * @param request
	 * @param response
	 */
	private void listPaper(HttpServletRequest request,
			HttpServletResponse response) {
		List<ExamPaperEntity> paperList = examPlanService.listPaper();
		print(response, FastjsonUtil.toJson(paperList));
	}

	/**
	 * 所有学生用户列表
	 * 
	 * @param request
	 * @param response
	 */
	private void listUser(HttpServletRequest request,
			HttpServletResponse response) {
		List<UserEntity> stuList = examPlanService.listUser();
		print(response, FastjsonUtil.toJson(stuList));
	}

	/**
	 * 考试安排查询
	 * 
	 * @param request
	 * @param response
	 */
	private void listExamPlan(HttpServletRequest request,
			HttpServletResponse response) {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		String sort = request.getParameter("sort");
		String order = request.getParameter("order");
		String type = request.getParameter("type");
		if (type == null) {
			type = "1";
		}
		String state = request.getParameter("state");
		String expire = request.getParameter("expire");
		int userId = SysuserUtil.getUserId(request);
		Map<Object, Object> paramMap = new HashMap<>();
		paramMap.put("page", page);
		paramMap.put("rows", rows);
		paramMap.put("sort", sort);
		paramMap.put("order", order);
		paramMap.put("type", type);
		paramMap.put("state", state);
		paramMap.put("expire", expire);
		paramMap.put("userId", userId);
		PageModel<ExamPlanEntity> examPlanList = examPlanService
				.listExamPlan(paramMap);
		print(response, FastjsonUtil.toJsonWithDateFormat(examPlanList,
				"yyyy-MM-dd HH:mm:ss"));
	}
}
